// pages/address/insertOrUpdateaddress/insertOrUpdateaddress.js
import {
   addressinsert,
   addressQuery,
   getAddressByaddressId,
   editMemberAddress,
   changeDefaultaddr
} from '@/miniprogram/pre_loadModules/addressModule/utils/Memberaddress'
import { getstorage, removeStorage, setstorage } from '@/miniprogram/utils/storage';
import {userBehavior} from "@/miniprogram/utils/behaviors/userBehavior"


Page({
   behaviors: [userBehavior],
   /**
    * 页面的初始数据
    */
   data: {
      addressBuff: [],
      currentChoose: [],
      isDetail: false,
      isPhone: false,
      isName: false,
      isSelect: false,
      address: {},
      id: null,
      memberId: null,
      name: null,
      phone: null,
      postCode: null,
      province: null,
      city: null,
      town: null,
      detailAddress: null,
      fullAddress: null,
      areaCode: null,
      defaultStatus: null,
      isDelete: null
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
      let obj = options
      let b = JSON.stringify(obj) == "{}"
      if (!b && options.addr) {
         let { province, city, town, detailAddress, fullAddress } = JSON.parse(options.addr);
         let getbuff = getstorage("buffInfo")
         this.setData({
            name: getbuff.name, phone: getbuff.phone,
            province, city, town, detailAddress, fullAddress,
            isDetail: true, isSelect: true, isName: getbuff.isName, isPhone: getbuff.isPhone
         })
         removeStorage("buffInfo")
      } else if (!b && options.id) {
         let id = options.id
         this.setData({
            id
         })
         this.getAddressByaddressIdFun(id)
      }
      else {
         this.setData({
            isDetail: false, isSelect: false
         })
      }
   },
   //https://boce.aliyun.com/detect/dns/5160ffeca0fc437182c34a442c80e35f
   onShow(){
      let {memberId,userInfo} = this.data
      memberId = userInfo.id
      console.log(memberId);
      this.setData({
         memberId
      })
   },
   phoneblur(e) {
      let isPhone = /^1[3456789]\d{9}$/.test(e.detail.value)
      if (e.detail.value == null || e.detail.value == '') {
         this.setData({
            isPhone: false
         })
         return;
      }
      else if (!isPhone) {
         this.setData({
            isPhone: false
         })
         return;
      } else {
         this.setData({
            isPhone: true
         })
      }
   },
   nameBlur(e) {
      let { name } = this.data
      if (name == null || name == '') {
         this.setData({
            isName: false
         })
         return;
      } else if (!isNaN(name)) {

      } else {
         this.setData({
            isName: true
         })
      }

   },
   detailAddrblur(e) {
      let dt = e.detail.value
      if (dt == null || dt == '') {
         this.setData({
            isDetail: false
         })
         return;
      } else {
         this.setData({
            isDetail: true
         })
      }
   },
   pickValueChange(e) {
      console.log(e);
      let address = this.data.address
      let province = e.detail.value[0]
      let city = e.detail.value[1]
      let town = e.detail.value[2]
      if (province == null || province == '' && city == null || city == '' && town == null || town == '') {
         this.setData({
            province, city, town,
            isSelect: false
         })
         return;
      } else {
         this.setData({
            province, city, town,
            isSelect: true
         })
      }
   },
   onChange(e) {
      var that = this
      that.getaddress()
      let status = e.detail.value;
      let { defaultStatus } = this.data
      defaultStatus = status === true ? 1 : 0
      let buff = getstorage("address")
      let changeData = buff.map(item => {
         item.defaultStatus = 0;
         return item;
      })
      that.changeDefaultaddrfun(changeData)
      that.setData({
         defaultStatus
      })
   },
   //添加地址提交
   formSubmit(e) {
      wx.showLoading()
      let { province, city, town, detailAddress, memberId,isSelect, isPhone, isDetail, isName, id } = this.data
      let addDate = { ...this.data, fullAddress: province + city + town + detailAddress, memberId: memberId }
      let editDate = { ...this.data, fullAddress: province + city + town + detailAddress, memberId: memberId, id: id }
      if (isName && isPhone && isSelect && isDetail && id == null) {
         this.insertAddress(addDate)
         addDate = null;
      } else if (isName && isPhone && isSelect && isDetail && id != null) {
         this.editMemberAddressfun(editDate)
         editDate = null;
      } else {

      }
      wx.hideLoading()
   },

   async insertAddress(data) {
      wx.showLoading()
      let res = await addressinsert(data)
      if (res.data.code == 200) {
         let res = this.getaddress()
         if (res) {
            wx.redirectTo({
               url: '/miniprogram/pre_loadModules/addressModule/pages/address/address',
            })
            this.resetData()
         }
      }
      wx.hideLoading()
   },
   async getaddress() {
      let {memberId} = this.data
      wx.showLoading()
      let res = await addressQuery(memberId)
      let addressBuff = res.data.data
      this.setData({
         addressBuff
      })
      removeStorage("address")
      setstorage("address", addressBuff)
      wx.hideLoading()
   },
   resetData() {
      this.setData({
         addressBuff: [],
         currentChoose: [],
         isDetail: false,
         isPhone: false,
         isName: false,
         isSelect: false,
         address: {},
         id: null,
         memberId: null,
         name: null,
         phone: null,
         postCode: null,
         province: null,
         city: null,
         town: null,
         detailAddress: null,
         fullAddress: null,
         areaCode: null,
         defaultStatus: null,
         isDelete: null
      })
   },
   async getAddressByaddressIdFun(data) {
      wx.showLoading()
      let addr = await getAddressByaddressId(data);
      if (addr.data.code == 200) {
         let { name, phone, province, city, town, detailAddress, fullAddress, defaultStatus } = addr.data.data
         this.setData({
            name, phone, province, city, town, detailAddress, fullAddress, defaultStatus,
            isDetail: true, isPhone: true, isName: true, isSelect: true
         })
      }
      wx.hideLoading()
   },
   poiSearch() {
      let { isName, isPhone, name, phone, defaultStatus } = this.data
      let buff = {}
      buff.isName = isName
      buff.isPhone = isPhone
      buff.name = name
      buff.phone = phone
      buff.defaultStatus = defaultStatus
      setstorage("buffInfo", buff)
      wx.redirectTo({
         url: '/miniprogram/pre_loadModules/addressModule/pages/baiduMap/poiSearch/poiSearch',
      })
   },
   async editMemberAddressfun(data) {
      wx.showLoading()
      let res = await editMemberAddress(data)
      if (res.data.code == 200) {
         wx.redirectTo({
            url: 'miniprogram/pre_loadModules/addressModule/pages/address/address',
         })
         this.resetData()
      }
      wx.hideLoading()
   },
   async changeDefaultaddrfun(data) {
      wx.showLoading()
      let res = await changeDefaultaddr(data)
      if (res.data.code == 200) {
         wx.showToast({
            title: '设置成功',
         })
      }
      wx.hideLoading()
   },
})